package com.tcm.ucenter.service;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.lang.IdcardUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.core.dozer.DozerConvert;
import com.tcm.ucenter.domain.dto.PatientDTO;
import com.tcm.ucenter.domain.entity.Patient;
import com.tcm.ucenter.domain.enums.BusinessCodeEnum;
import com.tcm.ucenter.mapper.PatientMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha")
public class PatientServiceImpl implements PatientService {

    @Autowired
    PatientMapper patientMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", check = false, retries = 3)
    private UidGenerator uidGenerator;

    @Override
    public PatientDTO findById(Long id) throws Exception {
        Patient patient = patientMapper.findById(id);
        PatientDTO patientVo = null;
        if (ObjectUtil.isNotNull(patient)) {
            patientVo = DozerConvert.copyProperties(patient, PatientDTO.class);
            patientVo.setAge(IdcardUtils.getAge(patientVo.getBirthday()));
        }
        return patientVo;
    }

    @Override
    public void save(PatientDTO patientDto) throws ServiceException {
        Patient checkPatient = patientMapper.findByUserPhone(patientDto.getUserId(), patientDto.getPhone(),
                patientDto.getName());
        if (ObjectUtil.isNotEmpty(checkPatient)) {
            R.throwFail(BusinessCodeEnum.PATIENT_HAS_EXISTS);
        }
        Patient patient = DozerConvert.copyProperties(patientDto, Patient.class);
        patient.setId(uidGenerator.getUID());
        patient.setCreateTime(LocalDateTime.now());
        patient.setUpdateTime(LocalDateTime.now());
        patientMapper.insert(patient);
    }

    @Override
    public void update(PatientDTO patientDto) throws ServiceException {
        Patient patient = patientMapper.findById(patientDto.getId());
        if (ObjectUtil.isNull(patient)) {
            R.throwFail(BusinessCodeEnum.PATIENT_NOT_EXISTS);
        }
        //检查患者是否重复添加
        Patient checkPatient = patientMapper.findByIdNamePhone(patientDto.getId(), patientDto.getPhone(), patientDto.getName());
        if (ObjectUtil.isNotEmpty(checkPatient)) {
            R.throwFail(BusinessCodeEnum.PATIENT_HAS_EXISTS);
        }
        Patient newPatient = DozerConvert.copyProperties(patientDto, Patient.class);
        newPatient.setUpdateTime(LocalDateTime.now());
        newPatient.setDeleted(new Byte("0"));
        patientMapper.update(newPatient);
    }

    @Override
    public void delete(Long id) {
        patientMapper.deleteById(id);
    }

    @Override
    public void deleteLogic(Long id) {
        patientMapper.deleteLogicById(id);
    }

    @Override
    public List<PatientDTO> listPatient(Long userId) {
        List<Patient> patients = patientMapper.listByUserId(userId);
        if (ObjectUtil.isNotEmpty(patients)) {
            return patients.stream().map(patient -> {
                PatientDTO patientDTO = new PatientDTO();
                BeanUtils.copyProperties(patient, patientDTO);
                try {
                    patientDTO.setAge(IdcardUtils.getAge(patient.getBirthday()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return patientDTO;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
